// 获取应用实例
const app = getApp()

Page({
  data: {
    swiperHeight: 200,
    // 轮播图
    banners: [{
      cover: app.apiRoot+ "content/images/banner.jpg",
      id: 1
    }],
    inventoryList: [],
    pageIndex: 0, // 当前页Index
    totalPages: 1, // 明细页数
    modules: [{
        name: '巡检管理',
        url: 'inspection/index',
        icon: 'xunjian',
        bg: '#ab96c5',
        role: '|Admin|Member|Register|Guest|'
      },
      {
        name: '报修管理',
        url: 'maintenance/index',
        icon: 'weixiu',
        bg: '#ab96c5',
        role: '|Admin|Member|Register|Guest|'
      },
      {
        name: '出库管理',
        url: 'stockout/index',
        icon: 'lingyong',
        bg: '#ab96c5',
        role: '|Admin|Member|Register|Guest|'
      },
      {
        name: '入库管理',
        url: 'stockin/index',
        icon: 'ruku',
        bg: '#ab96c5',
        role: '|Admin|Member|Register|Guest|'
      },
      {
        name: '备件查询',
        url: 'stock/index',
        icon: 'beijian',
        bg: '#ab96c5',
        role: '|Admin|Member|Register|Guest|'
      },
    ],
  },
  onLoad() {
    //设置swipe高度 2:1
    this.setData({
      swiperHeight: wx.getWindowInfo().windowWidth / 2,
      pageIndex: 0,
    });
  },

  loadInventory: function (push = false) {
    const _this = this;
    const {
      inventoryList,
      pageIndex: i,
      totalPages: t
    } = _this.data;
    if (push && i + 1 >= t) return;
    app.ajax({
      url: 'Home/GetMessages',
      data: {
        pageIndex: push ? i + 1 : i
      },
      verifyToken: false, // 首页默认显示盘点列表，不校验身份
      success(data) {
        if (data.result) {
          const {
            model = [], pageIndex = 0, totalPages = 1
          } = data;
          const list = push ? [...inventoryList, ...model || []] : model || [];
          _this.setData({
            inventoryList: list,
            pageIndex,
            totalPages
          });
        } else {
          app.showNotify(data.msg||'失败');
          return;
        }
      }
    });
  },

  onShow: function () {
    this.loadInventory();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadInventory(true);
  },

  // goAddInventory: function () {
  //   wx.navigateTo({
  //     url: '/pages/inventory/index',
  //   })
  // },
  goInventoryDetails: function (e) {
    const {
      target: {
        dataset: {
          url
        }
      }
    } = e;
    const {
      Token
    } = wx.getStorageSync(app.cache.AUTH_USER);
    if (!Token) {
      wx.clearStorageSync();
      app.getOpenId();//清除之后重新获取openId,不然登录页面获取不到openId
      wx.switchTab({
        url: '/pages/account/index'
      });
      return;
    }
    wx.navigateTo({
      url: url,
    })
  },

  goModule: function (e) {
    let _this = this;
    const _user = wx.getStorageSync(app.cache.AUTH_USER);
    if (_user && _user.Level != 'Guest') {
      _this.subscribeMessage();
      let url = this.data.modules[e.currentTarget.dataset.comIndex].url;
      wx.navigateTo({
        url: '../' + url
      })
    } else {
      wx.redirectTo({
        url: "/pages/bind/index"
      });
    }
  },

  //订阅消息
  subscribeMessage: function () {
    var templateId1 = 'nQpdDl04GLhjY7EwrF3rk6orDUEI_WcDQ3lp5FUg09k'; // 工单处理通知
    var templateId2 = 'nz2ht6PcAoYxU_3b4OJCDAHKuojrqx0s7UEau2cLkko'; // 待审批通知
    var templateId3 = 'kJ5Ag2dPBJ2Ra2MXLl0mQgFHuoNOdaDSboZ3zyLu1dM'; // 审批结果
    wx.requestSubscribeMessage({
      tmplIds: [templateId1,templateId2,templateId3],
      success(res) {
        // console.log(res);
        // var acceptResult = res[templateId1]; //'accept'、'reject'、'ban'
        // if (acceptResult == 'accept') {
        //   console.log("确认订阅");
        // }
        // wx.showModal({
        //   title: '您点击了按钮',
        //   content: '事件类型' + acceptResult + '\r\n' + '您将在几秒钟之后收到延迟的提示',
        //   showCancel: false,
        //   success: function () {
        //     if (acceptResult == 'accept') {
        //       wx.request({
        //         url: `${globalApp.apiRoot}/PublicProperty/SubscribeMessage`,
        //         method: 'POST',
        //         data: {
        //           sessionId: 1,
        //           templateId: 'nQpdDl04GLhjY7EwrF3rk6orDUEI_WcDQ3lp5FUg09k'
        //         },
        //         header: { 'content-type': 'application/x-www-form-urlencoded' },
        //         success(msgRes) {
        //           if (msgRes.data.success) {
        //             wx.showModal({
        //               title: '操作成功！',
        //               content: msgRes.data.msg,
        //             })
        //           } else {
        //             wx.showModal({
        //               title: '操作失败！',
        //               content: msgRes.data.msg,
        //             })
        //           }
        //         }
        //       })

        //     }
        //   }
        // })
      }
    })
  },
})